<!DOCTYPE HTML>
<html lang="en">
<head>
<title>Source code</title>
<link rel="stylesheet" type="text/css" href="../../../../../../../stylesheet.css" title="Style">
</head>
<body>
<main role="main">
<div class="sourceContainer">
<pre><span class="sourceLineNo">001</span><a id="line.1">/*</a>
<span class="sourceLineNo">002</span><a id="line.2"> * Licensed to the Apache Software Foundation (ASF) under one or more</a>
<span class="sourceLineNo">003</span><a id="line.3"> * contributor license agreements.  See the NOTICE file distributed with</a>
<span class="sourceLineNo">004</span><a id="line.4"> * this work for additional information regarding copyright ownership.</a>
<span class="sourceLineNo">005</span><a id="line.5"> * The ASF licenses this file to You under the Apache License, Version 2.0</a>
<span class="sourceLineNo">006</span><a id="line.6"> * (the "License"); you may not use this file except in compliance with</a>
<span class="sourceLineNo">007</span><a id="line.7"> * the License.  You may obtain a copy of the License at</a>
<span class="sourceLineNo">008</span><a id="line.8"> *</a>
<span class="sourceLineNo">009</span><a id="line.9"> *      http://www.apache.org/licenses/LICENSE-2.0</a>
<span class="sourceLineNo">010</span><a id="line.10"> *</a>
<span class="sourceLineNo">011</span><a id="line.11"> * Unless required by applicable law or agreed to in writing, software</a>
<span class="sourceLineNo">012</span><a id="line.12"> * distributed under the License is distributed on an "AS IS" BASIS,</a>
<span class="sourceLineNo">013</span><a id="line.13"> * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.</a>
<span class="sourceLineNo">014</span><a id="line.14"> * See the License for the specific language governing permissions and</a>
<span class="sourceLineNo">015</span><a id="line.15"> * limitations under the License.</a>
<span class="sourceLineNo">016</span><a id="line.16"> */</a>
<span class="sourceLineNo">017</span><a id="line.17">package org.apache.commons.dbutils.handlers.properties;</a>
<span class="sourceLineNo">018</span><a id="line.18"></a>
<span class="sourceLineNo">019</span><a id="line.19">import org.apache.commons.dbutils.PropertyHandler;</a>
<span class="sourceLineNo">020</span><a id="line.20"></a>
<span class="sourceLineNo">021</span><a id="line.21">import java.sql.Timestamp;</a>
<span class="sourceLineNo">022</span><a id="line.22">import java.util.Date;</a>
<span class="sourceLineNo">023</span><a id="line.23"></a>
<span class="sourceLineNo">024</span><a id="line.24">/**</a>
<span class="sourceLineNo">025</span><a id="line.25"> * {@link PropertyHandler} for date fields. Will convert {@link java.sql.Date}, {@link java.sql.Time}, and</a>
<span class="sourceLineNo">026</span><a id="line.26"> * {@link java.sql.Timestamp} from SQL types to java types.</a>
<span class="sourceLineNo">027</span><a id="line.27"> */</a>
<span class="sourceLineNo">028</span><a id="line.28">public class DatePropertyHandler implements PropertyHandler {</a>
<span class="sourceLineNo">029</span><a id="line.29">    @Override</a>
<span class="sourceLineNo">030</span><a id="line.30">    public boolean match(final Class&lt;?&gt; parameter, final Object value) {</a>
<span class="sourceLineNo">031</span><a id="line.31">        if (value instanceof Date) {</a>
<span class="sourceLineNo">032</span><a id="line.32">            final String targetType = parameter.getName();</a>
<span class="sourceLineNo">033</span><a id="line.33">            if ("java.sql.Date".equals(targetType)) {</a>
<span class="sourceLineNo">034</span><a id="line.34">                return true;</a>
<span class="sourceLineNo">035</span><a id="line.35">            } else</a>
<span class="sourceLineNo">036</span><a id="line.36">            if ("java.sql.Time".equals(targetType)) {</a>
<span class="sourceLineNo">037</span><a id="line.37">                return true;</a>
<span class="sourceLineNo">038</span><a id="line.38">            } else</a>
<span class="sourceLineNo">039</span><a id="line.39">            if ("java.sql.Timestamp".equals(targetType)</a>
<span class="sourceLineNo">040</span><a id="line.40">                    &amp;&amp; !Timestamp.class.isInstance(value)) {</a>
<span class="sourceLineNo">041</span><a id="line.41">                return true;</a>
<span class="sourceLineNo">042</span><a id="line.42">            }</a>
<span class="sourceLineNo">043</span><a id="line.43">        }</a>
<span class="sourceLineNo">044</span><a id="line.44"></a>
<span class="sourceLineNo">045</span><a id="line.45">        return false;</a>
<span class="sourceLineNo">046</span><a id="line.46">    }</a>
<span class="sourceLineNo">047</span><a id="line.47"></a>
<span class="sourceLineNo">048</span><a id="line.48">    @Override</a>
<span class="sourceLineNo">049</span><a id="line.49">    public Object apply(final Class&lt;?&gt; parameter, Object value) {</a>
<span class="sourceLineNo">050</span><a id="line.50">        final String targetType = parameter.getName();</a>
<span class="sourceLineNo">051</span><a id="line.51">        final Date dateValue = (Date) value;</a>
<span class="sourceLineNo">052</span><a id="line.52">        final long time = dateValue.getTime();</a>
<span class="sourceLineNo">053</span><a id="line.53"></a>
<span class="sourceLineNo">054</span><a id="line.54">        if ("java.sql.Date".equals(targetType)) {</a>
<span class="sourceLineNo">055</span><a id="line.55">            value = new java.sql.Date(time);</a>
<span class="sourceLineNo">056</span><a id="line.56">        } else</a>
<span class="sourceLineNo">057</span><a id="line.57">        if ("java.sql.Time".equals(targetType)) {</a>
<span class="sourceLineNo">058</span><a id="line.58">            value = new java.sql.Time(time);</a>
<span class="sourceLineNo">059</span><a id="line.59">        } else</a>
<span class="sourceLineNo">060</span><a id="line.60">        if ("java.sql.Timestamp".equals(targetType)) {</a>
<span class="sourceLineNo">061</span><a id="line.61">            value = new Timestamp(time);</a>
<span class="sourceLineNo">062</span><a id="line.62">        }</a>
<span class="sourceLineNo">063</span><a id="line.63"></a>
<span class="sourceLineNo">064</span><a id="line.64">        return value;</a>
<span class="sourceLineNo">065</span><a id="line.65">    }</a>
<span class="sourceLineNo">066</span><a id="line.66">}</a>




























































</pre>
</div>
</main>
</body>
</html>
